import 'package:flutter_menu/data/obx_store.dart';
import 'package:objectbox/objectbox.dart';
import 'package:json_annotation/json_annotation.dart';

import '../objectbox.g.dart';
part 'material_model.g.dart';


@Entity()
@JsonSerializable()
class MaterialModel with IEntity{
  @Id()
  int obxId;

  ///成本价格
  double? costPrice;

  ///原料分类id
  String? materialCategoryId;

  ///原料id
  String? materialId;

  ///原料名称
  String? materialName;

  ///原料/半成品第二语言
  String? materialSecondName;

  ///计量单位id
  int? materialType;

  ///计量单位id
  int? measureId;

  ///字典表计量单位
  String? measureName;

  ///计量
  int? measureValue;

  ///店铺id
  int? shopId;

  ///库存件数
  double? stockNumber;

  ///库存量
  double? stockQuantity;

  ///更新时间
  int updateTime;

  MaterialModel({
    this.obxId =0,
     this.costPrice,
     this.materialCategoryId,
     this.materialId,
     this.materialName,
     this.materialSecondName,
     this.materialType,
     this.measureId,
     this.measureName,
     this.measureValue,
     this.shopId,
     this.stockNumber,
     this.stockQuantity,
     this.updateTime = 0,
  });

  factory MaterialModel.fromJson(Map<String,dynamic> json) => _$MaterialModelFromJson(json);
  Map<String, dynamic> toJson() => _$MaterialModelToJson(this);

  static ToOne<MaterialModel> toOneFromJson(Map<String, dynamic> json) =>
  ToOne(target: MaterialModel.fromJson(json));

  static Map<String, dynamic>? toOneJson(ToOne<MaterialModel> s) =>
  s.target != null ? _$MaterialModelToJson(s.target!) : {};

  static ToMany<MaterialModel> toManyFromJson(List<dynamic>? json) =>
  ToMany(items: json?.map((e) => MaterialModel.fromJson(e)).toList());

  static List<Map<String, dynamic>>? toManyJson(ToMany<MaterialModel>? s) =>
  s?.toList().map((e) => e.toJson()).toList();

  @override
  int update(Store store, int? time) {
    updateTime = time?? updateTime;
    var box =  store.box<MaterialModel>();
    return box.put(this);
  }

}


@Entity()
@JsonSerializable()
class MaterialDeptStockModel with IEntity{
  @Id()
  int obxId;

  ///部门（仓库）id
  String? deptId;

  ///原料id
  String? materialId;

  ///库存件数
  double? stockNumber;

  ///库存数量
  double? stockQuantity;

  ///修改时间
  int updateTime;

  MaterialDeptStockModel({
    this.obxId =0,
     this.deptId,
     this.materialId,
     this.stockNumber,
     this.stockQuantity,
     this.updateTime = 0,
  });


  factory MaterialDeptStockModel.fromJson(Map<String,dynamic> json) => _$MaterialDeptStockModelFromJson(json);
  Map<String, dynamic> toJson() => _$MaterialDeptStockModelToJson(this);

  static ToOne<MaterialDeptStockModel> toOneFromJson(Map<String, dynamic> json) =>
  ToOne(target: MaterialDeptStockModel.fromJson(json));

  static Map<String, dynamic>? toOneJson(ToOne<MaterialDeptStockModel> s) =>
  s.target != null ? _$MaterialDeptStockModelToJson(s.target!) : {};

  static ToMany<MaterialDeptStockModel> toManyFromJson(List<dynamic>? json) =>
  ToMany(items: json?.map((e) => MaterialDeptStockModel.fromJson(e)).toList());

  static List<Map<String, dynamic>>? toManyJson(ToMany<MaterialDeptStockModel>? s) =>
  s?.toList().map((e) => e.toJson()).toList();

  @override
  int update(Store store, int? time) {
    updateTime = time?? updateTime;
    var box =  store.box<MaterialDeptStockModel>();
    return box.put(this);
  }

}


@Entity()
@JsonSerializable()
class OptionMaterialModel with IEntity {
  @Id()
  int obxId;

  ///配方id
  String? formulaId;

  ///原料信息
  @JsonKey(fromJson: OptionMaterialListFormulaMaterial.toManyFromJson, toJson: OptionMaterialListFormulaMaterial.toManyJson)
  ToMany<OptionMaterialListFormulaMaterial> formulaMaterials = ToMany<OptionMaterialListFormulaMaterial>();

  ///选项id
  String? optionId;

  ///选项名
  String? optionName;

  ///菜品id
  String? productUuid;

  ///规格id
  String? specsUuid;

  OptionMaterialModel({
    this.obxId =0,
    this.updateTime = 0,
     this.formulaId,
     this.optionId,
     this.optionName,
     this.productUuid,
     this.specsUuid,
  });

  factory OptionMaterialModel.fromJson(Map<String,dynamic> json) => _$OptionMaterialModelFromJson(json);
  Map<String, dynamic> toJson() => _$OptionMaterialModelToJson(this);

  static ToOne<OptionMaterialModel> toOneFromJson(Map<String, dynamic> json) =>
  ToOne(target: OptionMaterialModel.fromJson(json));

  static Map<String, dynamic>? toOneJson(ToOne<OptionMaterialModel> s) =>
  s.target != null ? _$OptionMaterialModelToJson(s.target!) : {};

  static ToMany<OptionMaterialModel> toManyFromJson(List<dynamic>? json) =>
  ToMany(items: json?.map((e) => OptionMaterialModel.fromJson(e)).toList());

  static List<Map<String, dynamic>>? toManyJson(ToMany<OptionMaterialModel>? s) =>
  s?.toList().map((e) => e.toJson()).toList();

  @override
  int updateTime;

  @override
  int update(Store store, int? time) {
    updateTime = time?? updateTime;
    formulaMaterials.forEach((e)=> e.obxId = e.update(store,time));
    var box =  store.box<OptionMaterialModel>();
    return box.update(this, OptionMaterialModel_.optionId, optionId);
  }

}


@Entity()
@JsonSerializable()
class OptionMaterialListFormulaMaterial  with IEntity{
  @Id()
  int obxId;

  ///原料配方关联id
  String? formulaMaterialId;

  ///原料id
  String? materialId;

  ///原料名
  String? materialName;

  ///单位
  String? measureName;

  ///使用量
  double? usageAmount;

  OptionMaterialListFormulaMaterial({
    this.obxId = 0,
    this.updateTime = 0,
     this.formulaMaterialId,
     this.materialId,
     this.materialName,
     this.measureName,
     this.usageAmount,
  });

  factory OptionMaterialListFormulaMaterial.fromJson(Map<String,dynamic> json) => _$OptionMaterialListFormulaMaterialFromJson(json);
  Map<String, dynamic> toJson() => _$OptionMaterialListFormulaMaterialToJson(this);

  static ToOne<OptionMaterialListFormulaMaterial> toOneFromJson(Map<String, dynamic> json) =>
  ToOne(target: OptionMaterialListFormulaMaterial.fromJson(json));

  static Map<String, dynamic>? toOneJson(ToOne<OptionMaterialListFormulaMaterial> s) =>
  s.target != null ? _$OptionMaterialListFormulaMaterialToJson(s.target!) : {};

  static ToMany<OptionMaterialListFormulaMaterial> toManyFromJson(List<dynamic>? json) =>
  ToMany(items: json?.map((e) => OptionMaterialListFormulaMaterial.fromJson(e)).toList());

  static List<Map<String, dynamic>>? toManyJson(ToMany<OptionMaterialListFormulaMaterial>? s) =>
  s?.toList().map((e) => e.toJson()).toList();

  @override
  int updateTime;

  @override
  int update(Store store, int? time) {
    updateTime = time ?? updateTime;
    var box = store.box<OptionMaterialListFormulaMaterial>();
    return box.put(this);
  }
}
